//
//  BaseNavigationCtroller.m
//  SpeedBorrow
//
//  Created by  刘尚威 on 16/8/26.
//  Copyright © 2016年  刘尚威. All rights reserved.
//

#import "XLNavigationController.h"
#import "XLMainDefine.h"
@interface XLNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@end

@implementation XLNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.enableRightGesture = YES;
    [self configNavBarTheme];
    
}

- (void)configNavBarTheme {
    //定义导航颜色 在创建视图 实际位置要+64,如果设置了self.navigationBar.translucent = NO,和setImage效果一样
    self.navigationBar.tintColor = [UIColor whiteColor];//设置按钮颜色
    self.navigationBar.translucent = NO;
    
    //调用系统相册时 导航栏颜色可以跟着变换
    UIColor *navBarColor = [UIColor colorWithRed:56/255.0 green:153/255.0 blue:231/255.0 alpha:1.0];
    [[UINavigationBar appearance] setBarTintColor:navBarColor];
    
    //状态栏字体改为白色  info.plist中添加一个字段：view controller -base status bar 设置为NO
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
    // 设置导航栏的标题颜色，字体
    NSDictionary *titleDic = @{
                               NSFontAttributeName : [UIFont boldSystemFontOfSize:18],
                               NSForegroundColorAttributeName : [UIColor whiteColor]
                               };
    
    [self.navigationBar setTitleTextAttributes:titleDic];
    
    
//    //去掉/隐藏 navi返回按钮的文字
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100) forBarMetrics:UIBarMetricsDefault];
    
    //设置navigationbar返回图片
    UIImage *image = XLImage(@"nav_backArrow.png");
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationBar.backIndicatorImage = image;
    self.navigationBar.backIndicatorTransitionMaskImage = image;
    
    //去掉导航栏底部阴影
    [self.navigationBar setShadowImage:[[UIImage alloc]init]];
    
    //系统返回手势
    __weak XLNavigationController *weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
        self.delegate = weakSelf;
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}

#pragma mark - override
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (self.childViewControllers.count > 0) {
        
        [viewController setHidesBottomBarWhenPushed:YES];
    }
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    [super pushViewController:viewController animated:animated];
    
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    return [super popToRootViewControllerAnimated:animated];
}
- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    return [super popToViewController:viewController animated:animated];
}

#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer isEqual:self.interactivePopGestureRecognizer] && self.viewControllers.count > 1 && [self.visibleViewController isEqual:[self.viewControllers lastObject]]) {
        //判断当导航堆栈中存在页面，并且可见视图如果不是导航堆栈中的最后一个视图时，就会屏蔽掉滑动返回的手势。此设置是为了避免页面滑动返回时因动画存在延迟所导致的卡死。
        return self.enableRightGesture;
    } else {
        return NO;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
